<template>
  <div class="ui container">
    <div class="ui segment">
      <h3 class="ui teal header left aligned">修改密码</h3>
      <el-divider></el-divider>
      <div class="ui left aligned container">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="原密码" prop="beforePass">
            <el-col :span="8">
              <el-input type="password" v-model="ruleForm.beforePass" autocomplete="off" show-password></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-col :span="8">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off" show-password></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-col :span="8">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" show-password></el-input>
            </el-col>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')" round>提交</el-button>
            <el-button @click="resetForm('ruleForm')" round>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        beforePass: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        beforePass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let salt = "1a2b3c4d";
          let str1 =
            "" +
            salt.charAt(0) +
            salt.charAt(2) +
            this.ruleForm.beforePass +
            salt.charAt(5) +
            salt.charAt(4);
          let str2 =
            "" +
            salt.charAt(0) +
            salt.charAt(2) +
            this.ruleForm.pass +
            salt.charAt(5) +
            salt.charAt(4);
          //请求后端登录接口
          let param = new URLSearchParams();
          param.append("oldPassword", md5(str1));
          param.append("newPassword", md5(str2));
          this.$https({
            method: 'post',
            url: '/user/updatePassword',
            params: param
          }).then(res => {
            if (res.data.code == 200) {
              this.$message.success('修改密码成功！请重新登录！');
              this.$router.replace('/');
            } else {
              this.$message.success('res.data.data');
            }
          })
        } else {
          this.$message.error("请将数据填写完整！");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
</style>